/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.action;

import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.lineage.server.utils.IterableElementList;
import com.lineage.server.utils.collections.Lists;

/**
 * Class <code>L1ItemXmlParser</code> ItemXml分析器.
 * 
 * @author jrwz
 * @version 2012-6-20下午06:28:43
 * @see com.lineage.server.model.item.action
 * @since JDK1.7
 */
public final class L1ItemXmlParser {
    /**
     * 取得布尔属性.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @param name
     *            - 名称
     * @param defaultValue
     *            - 默认值
     * @return 结果
     */
    public static boolean getBoolAttribute(final Element element,
            final String name, final boolean defaultValue) {
        boolean result = defaultValue;
        final String value = element.getAttribute(name);
        if (!value.equals("")) {
            result = Boolean.valueOf(value);
        }
        return result;
    }

    /**
     * 取得double属性.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @param name
     *            - 名称
     * @param defaultValue
     *            - 默认值
     * @return 结果
     */
    public static double getDoubleAttribute(final Element element,
            final String name, final double defaultValue) {
        double result = defaultValue;
        try {
            result = Double.valueOf(element.getAttribute(name));
        } catch (final NumberFormatException e) {
        }
        return result;
    }

    /**
     * 取得按标记名称的第一个子元素.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @param tagName
     *            - 目标名称
     * 
     * @return 没有则返回null
     */
    public static Element getFirstChildElementByTagName(final Element element,
            final String tagName) {
        final IterableElementList list = new IterableElementList(
                element.getElementsByTagName(tagName));
        for (final Element elem : list) {
            return elem;
        }
        return null;
    }

    /**
     * 取得INT属性.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @param name
     *            - 名称
     * @param defaultValue
     *            - 默认值
     * @return 结果
     */
    public static int getIntAttribute(final Element element, final String name,
            final int defaultValue) {
        int result = defaultValue;
        try {
            result = Integer.valueOf(element.getAttribute(name));
        } catch (final NumberFormatException e) {
        }
        return result;
    }

    /**
     * 取得String属性.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @param name
     *            - 名称
     * @param defaultValue
     *            - 默认值
     * @return 结果
     */
    public static String getStringAttribute(final Element element,
            final String name, final String defaultValue) {
        String result = defaultValue;
        try {
            result = String.valueOf(element.getAttribute(name));
        } catch (final NumberFormatException e) {
        }
        return result;
    }

    /**
     * 动作列表集合.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @return 结果
     */
    public static List<L1ItemAction> listActions(final Element element) {
        final List<L1ItemAction> result = Lists.newList();
        final NodeList list = element.getChildNodes();
        for (final Element elem : new IterableElementList(list)) {
            final L1ItemAction action = L1ItemActionFactory.newAction(elem);
            if (action != null) {
                result.add(action);
            }
        }
        return result;
    }

    /** ItemXml分析器. */
    private L1ItemXmlParser() {
    }
}
